package com.exampleyyy.demoyyy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.exampleyyy.demoyyy.JieKouService.UserServiceJieKou;
import com.exampleyyy.demoyyy.ShiTiYYY.User;
import com.exampleyyy.demoyyy.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.profile.DefaultProfile;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RequestMapping("/user")
@RestController
@Slf4j
public class UserController {
    // 1. 普通类
    // 2. @RestController添加装饰器(注解),变成控制器类
    // 3. @RequestMapping捕捉主路由是 /user的请求
    // 4. @Slf4j添加日志记录
    // 5. 捕捉 "/sendMsg" 子路由
    @Autowired
    private UserServiceJieKou fuwuuserjiekou;
    @PostMapping("/sendMsg")
    public R<Object> sendMsg(@RequestBody User user, HttpSession session){
        // {"phone":"18311391491"}
        // 1.@RequestBody设置参数,接收手机号 将json数据赋值给User实体类对象 user
        log.info("获取前端的手机号是"+user.getPhone());

        // 2.生成六位随机验证码
        Integer param = null;
        param = new Random().nextInt(999999);//生成随机数，最大为999999
        if(param < 100000){
            param = param + 100000;//保证随机数为6位数字
        }
        log.info("生成的随机码是"+param);
        session.setAttribute(user.getPhone(),param);
        log.info("短信发送成功");
        return R.success(1,"短信发送成功",null);

    };
    // 用户登录函数
    @PostMapping("/login")
    public R<Object> login(@RequestBody Map QianDuanMa, HttpSession session) {
        // 拿到前端带来的手机号和验证码
        String QDphone = QianDuanMa.get("phone").toString();
        String QDcode = QianDuanMa.get("code").toString();
        log.info("前端码是" + QDcode);

        // 拿到后台存储的验证码进行比对
        Object HTcode = session.getAttribute(QDphone);
        if (HTcode == null) {
            return R.error(0, "请先获取验证码", null);
        }

        if (!HTcode.toString().equals(QDcode)) {
            return R.error(0, "验证码错误,请重新输入", null);
        }

        log.info("验证码比对成功");

        // 查询用户是否已存在
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getPhone, QDphone);
        List<User> userList = fuwuuserjiekou.list(lambdaQueryWrapper);
        User user = userList.isEmpty() ? null : userList.get(0);

        if (user == null) {
            // 用户不存在则新增
            user = new User();
            user.setPhone(QDphone);
            user.setStatus(1);
            user.setAvatar("默认头像");
            fuwuuserjiekou.save(user);
        }

        // 登录成功，设置 session
        session.setAttribute("用户信息id", user.getId());
        return R.success(1, "验证码验证成功,可以登录", null);
    }
}